package gcontent.action;

import java.util.Calendar;
import java.text.NumberFormat;
import java.util.Locale;
import com.opensymphony.xwork2.ActionSupport;

import gcontent.db.DBParams;
import gcontent.db.statistics.*;

public class Stats extends ActionSupport { 
	private static final long serialVersionUID = 1L;	
	private static final NumberFormat numberFormat = NumberFormat.getInstance(Locale.GERMAN);
	
	// Page labels
	private String pageTitle = "Web";
	private String daysRunning = "Days running:";
	
	// Visits labels
	private String visitsTitle = "Visits";
	private String visitsLabel = "Total visits number:";	
	private String visitsMonthLabel = "Visits during this month:";
	private String visitsYearLabel = "Visits during this year (xxxx):";
	private String averageVisits = "Average of visits per day:";
	
	// Searches labels
	private String searchsTitle = "Searches";
	private String searchsLabel = "Total number of searches:";
	private String searchsMonthLabel = "Searches during this month:";
	private String searchsYearLabel = "Searches during this year (xxxx):";
	private String searchsMostSearchedLabel = "Most searched query:";
	private String searchsContent = "Times that someone have searched html content:";
	private String searchsImages = "Times that someone have searched images:";	
	private String searchsVideos = "Times that someone have searched videos:";
	private String searchsTorrents = "Times that someone have searched torrent files:";
	private String searchsELinks = "Times that someone have searched eLinks:";
	private String averageSearches = "Average of searches per day:";
	
	// Visit values
	private float totalVisitsNumber = 0;
	private int thisMonthVisitsNumber = 0;
	private int currentYearVisitsNumber = 0;
	private float avgVisits = 0;
	
	// Searches values
	private float totalSearchesNumber = 0;
	private int thisMonthSearchesNumber = 0;
	private int currentYearSearchesNumber = 0;
	private int numSearchesContent = 0;
	private int numSearchesImages = 0;	
	private int numSearchesVideos = 0;
	private int numSearchesTorrents = 0;
	private int numSearchesELinks = 0;
	private float avgSearches = 0;
	private String mostSearchedString = "";
	private int mostSearchedString_n = 0;
	
	// Page values
	private long numDaysRunning = 0; 
	
	public String execute() {
		this.countDaysRunning();
		this.countVisits();	
		this.countSearches();
		return SUCCESS;
	}

	private void countSearches() {
		try {
			Statistics st = new Statistics();
			DBParams [] params = st.countSearches();
			if (params != null) {
				try {
					totalSearchesNumber = Integer.parseInt(String.valueOf(params[4].getRetValue()));
				} catch (Exception ex) {
					totalSearchesNumber = 0;
				}
				
				try {
					thisMonthSearchesNumber = Integer.parseInt(String.valueOf(params[5].getRetValue()));
				} catch (Exception ex) {
					thisMonthSearchesNumber = 0;
				}
				
				try {
					currentYearSearchesNumber = Integer.parseInt(String.valueOf(params[6].getRetValue()));
				} catch (Exception ex) {
					currentYearSearchesNumber = 0;
				}
				
				try {
					mostSearchedString = String.valueOf(params[7].getRetValue());
				} catch (Exception ex) {
					mostSearchedString = "";
				}
				
				try {
					mostSearchedString_n = Integer.parseInt(String.valueOf(params[8].getRetValue()));
				} catch (Exception ex) {
					mostSearchedString_n = 0;
				}
				
				try {
					numSearchesContent = Integer.parseInt(String.valueOf(params[9].getRetValue()));
				} catch (Exception ex) {
					numSearchesContent = 0;
				}
				
				try {
					numSearchesImages = Integer.parseInt(String.valueOf(params[10].getRetValue()));
				} catch (Exception ex) {
					numSearchesImages = 0;
				}
				
				try {
					numSearchesVideos = Integer.parseInt(String.valueOf(params[11].getRetValue()));
				} catch (Exception ex) {
					numSearchesVideos = 0;
				}
				
				try {
					numSearchesTorrents = Integer.parseInt(String.valueOf(params[12].getRetValue()));
				} catch (Exception ex) {
					numSearchesTorrents = 0;
				}
				
				try {
					numSearchesELinks = Integer.parseInt(String.valueOf(params[13].getRetValue()));
				} catch (Exception ex) {
					numSearchesELinks = 0;
				}
				
				try {			
					avgSearches = totalSearchesNumber / numDaysRunning;
				} catch (Exception ex) {
					avgVisits = 0;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void countDaysRunning() {
		try {
			Statistics st = new Statistics();
			DBParams [] params = st.countDaysRunning();
			if (params != null) {
				try {
					java.sql.Date date;
					Calendar cal = Calendar.getInstance();
					date = java.sql.Date.valueOf(String.valueOf(params[0].getRetValue()));
					numDaysRunning = ((((cal.getTime().getTime() - date.getTime()) / 1000) / 3600) / 24);
				} catch (Exception ex) {
					numDaysRunning = 0;
				}				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void countVisits() {
		try {
			Statistics st = new Statistics();
			DBParams [] params = st.countVisits();
			if (params != null) {
				try {
					totalVisitsNumber = Integer.parseInt(String.valueOf(params[4].getRetValue()));
				} catch (Exception ex) {
					totalVisitsNumber = 0;
				}
				
				try {
					thisMonthVisitsNumber = Integer.parseInt(String.valueOf(params[5].getRetValue()));
				} catch (Exception ex) {
					thisMonthVisitsNumber = 0;
				}
				
				try {
					currentYearVisitsNumber = Integer.parseInt(String.valueOf(params[6].getRetValue()));
				} catch (Exception ex) {
					currentYearVisitsNumber = 0;
				}
				
				try {			
					avgVisits = totalVisitsNumber / numDaysRunning;
				} catch (Exception ex) {
					avgVisits = 0;
				}				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	// Values
	public String getTotalVisitsNumber() {
		return numberFormat.format(totalVisitsNumber);
	}
	
	public int getCurerentMonthVisitsNumber() {
		return thisMonthVisitsNumber;
	}
	
	public int getCurrentYearVisitsNumber() {
		return currentYearVisitsNumber;
	}
	
	public String getAvgVisits() {				
		return numberFormat.format(avgVisits);
	}
	
	public String getTotalSearchesNumber() {
		return numberFormat.format(totalSearchesNumber);
	}
		
	public int getThisMonthSearchesNumber() {
		return thisMonthSearchesNumber;
	}
	
	public int getCurrentYearSearchesNumber() {
		return currentYearSearchesNumber;
	}
	
	public String getMostSearchedQuery() {
		if (mostSearchedString_n <= 3)
			return "-";
		else
			return "'" + mostSearchedString + "' was requested for " + mostSearchedString_n + " times";
	}
	
	public int getNumSearchesContent() {
		return numSearchesContent;
	}
	
	public int getNumSearchesImages() {
		return numSearchesImages;
	}
	
	public int getNumSearchesVideos() {
		return numSearchesVideos;
	}
		
	public int getNumSearchesTorrents() {
		return numSearchesTorrents;
	}
	
	public int getNumSearchesELinks() {
		return numSearchesELinks;
	}
	
	public String getAvgSearches() {
		return numberFormat.format(avgSearches);
	}
	
	public String getNumDaysRunning() {
		return numberFormat.format(numDaysRunning);
	}
	
	// Labels
	public String getPageTitle() {
		return pageTitle;
	}
	
	public String getVisitsLabel() {
		return this.visitsLabel;
	}
	
	public String getVisitsTitle() {
		return visitsTitle;
	}
	
	public String getAverageVisits() {
		return averageVisits;
	}
	
	public String getVisitsMonthLabel() {
		return visitsMonthLabel;
	}
	
	public String getVisitsYearLabel() {
		return visitsYearLabel.replace("xxxx", String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
	}
	
	
	public String getSearchsTitle() {
		return searchsTitle;
	}

	public String getSearchsLabel() {
		return searchsLabel;
	}

	public String getSearchsMonthLabel() {
		return searchsMonthLabel;
	}

	public String getSearchsYearLabel() {
		return searchsYearLabel;
	}

	public String getSearchsMostSearchedLabel() {
		return searchsMostSearchedLabel;
	}

	public String getSearchsContent() {
		return searchsContent;
	}

	public String getSearchsImages() {
		return searchsImages;
	}

	public String getSearchsVideos() {
		return searchsVideos;
	}

	public String getSearchsTorrents() {
		return searchsTorrents;
	}

	public String getSearchsELinks() {
		return searchsELinks;
	}
	
	public String getAverageSearches() {
		return averageSearches;
	}
	
	public String getDaysRunning() {
		return daysRunning;
	}
}
